package com.vhukze.basics.entity.dto;

import com.vhukze.basics.enumeration.RespCodeEnum;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class RestfulResp<T> {
    private Boolean success;
    private int code;
    private String message;
    private T data;

    // 未知异常
    public RestfulResp<T> unknown(String message) {
        this.success = false;
        this.code = RespCodeEnum.UNKNOWN.getCode();
        if (message == null || "".equals(message)) {
            this.message = RespCodeEnum.UNKNOWN.getMessage();
        } else {
            this.message = message;
        }
        return this;
    }

    // 自定义编号消息
    public RestfulResp<T> error(String message, int code) {
        this.success = false;
        this.code = code;
        this.message = message;
        return this;
    }

    // 未知异常
    public RestfulResp<T> unknown() {
        return this.unknown("失败");
    }

    // 成功，但不返回data数据
    public RestfulResp<T> success() {
        this.success = true;
        this.message = RespCodeEnum.SUCCESS.getMessage();
        this.code = RespCodeEnum.SUCCESS.getCode();
        return this;
    }

    // 成功且返回data数据
    public RestfulResp<T> success(T data) {
        this.success = true;
        this.message = RespCodeEnum.SUCCESS.getMessage();
        this.code = RespCodeEnum.SUCCESS.getCode();
        this.data = data;
        return this;
    }

    // 成功且返回data数据 自定义返回消息
    public RestfulResp<T> success(T data, String message) {
        this.success = true;
        this.message = message;
        this.code = RespCodeEnum.SUCCESS.getCode();
        this.data = data;
        return this;
    }

    public RestfulResp<T> enumResp(RespCodeEnum respCodeEnum) {
        this.success = false;
        this.message = respCodeEnum.getMessage();
        this.code = respCodeEnum.getCode();
        return this;
    }

}